﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace QLKS
{
    public partial class frDoiMatKhau : Form
    {
        public frDoiMatKhau()
        {
            InitializeComponent();
        }

        private DTO.CTAIKHOAN _tK;

        internal DTO.CTAIKHOAN TK
        {
            get { return _tK; }
            set { _tK = value; }
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            if (txtOldPass.Text == "" || txtOldPass.Text == "" || txtRetypePass.Text == "")
            {
                MessageBox.Show("Ban chua nhap het cac o trong.");
            } 
            else
            {
                if (txtNewPass.Text != txtRetypePass.Text)
                {
                    MessageBox.Show("Mat khau moi va xac nhan mat khau khong khop.");
                }
                else
                {
                    String MD5TKcu = BUS.XuLyTaiKhoan.getMd5Hash(txtOldPass.Text);
                    DTO.CTAIKHOAN taikhoanmoi = new DTO.CTAIKHOAN();
                    taikhoanmoi.Username = this.TK.Username;
                    taikhoanmoi.Password = BUS.XuLyTaiKhoan.getMd5Hash(txtNewPass.Text);
                    taikhoanmoi.MaNhanVien = this.TK.MaNhanVien;

                    DTO.CTAIKHOAN taikhoancu = new DTO.CTAIKHOAN();
                    taikhoancu = BUS.XuLyTaiKhoan.TimTaiKhoanTheoUsername(this.TK.Username, MD5TKcu);

                    if (taikhoancu.MaNhanVien != 0)
                    {
                        if (!(BUS.XuLyTaiKhoan.SuaTaiKhoan(taikhoanmoi,this.TK.MaNhanVien)))// || !(BUS.XuLyTaiKhoan.ThemTaiKhoan(taikhoanmoi)))
                        {
                            MessageBox.Show("Co loi xay ra.");
                        }
                        this.Close();
                    }
                    else
                        MessageBox.Show("Mat khau khong dung.");
                }
            }
        }

    }
}
